/**
 * Created by Administrator on 2018/4/13.
 */
const todos = (state = [], action) => {
	switch (action.type) {
		case 'ADD_TODO':
			return [
				...state,
				{
					id: action.id,
					text: action.text,
					completed: false
				}
			]
		case 'TOGGLE_TODO':
			return state.map(
				todo => (todo.id === action.id) ? {...todo, completed: !todo.completed} : todo
			);
		case 'DELETE_TODO':
			state.splice(state.findIndex(item => item.id === action.id), 1);
			// 不直接修改state
			return [
				...state
			];
		default:
			return state;
	}
}

export default todos;